// pages/resourceList/resourceList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req:false,
    title: '通知',
    search: {
      pageNumber: 1,
      pageSize: 10,
      totalPages: 0
    },
    list: [],
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.band) {
      return;
    }
    var url = dataset.turn;
    if (dataset.id) {
      url += "?id=" + dataset.id
    }
    wx.navigateTo({
      url: url
    })
  },
  org(e) {
    console.log('org')
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  user(e) {
    var that = this;
    var id = e.currentTarget.dataset.createuid;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    console.log(dataset)
    if (dataset.band) {
      return;
    }
    var url = dataset.turn;
    if (dataset.id && dataset.orgid) {
      url = url + "?id=" + dataset.id + "&orgId=" + that.data.orgId
    } else if (dataset.id) {
      url += "?id=" + dataset.id
    }
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  // 组织删除
  orgDelete(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '请确认是否删除',
      success(res) {
        if (res.confirm) {
          var relationId = e.currentTarget.dataset.id;
          app.req({
            url: "orgmini/activity/orgDeleteAct",
            data: {
              "orgId": that.data.search.orgId,
              "type": that.data.search.messageType,
              "relationId": relationId,
            },
            success: function (res) {
              if (200 == res.code) {
                that.list();
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                });
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toggle("search.orgId", options.id);
    this.toggle("search.messageType", options.type);
    switch (options.type * 1) {
      case 1:
        this.toggle("title", '资讯');
        this.toggle("nopic", 'zixun');
        wx.setNavigationBarTitle({
          title: '资讯列表'
        })
        break;
      case 3:
        this.toggle("title", '通知');
        this.toggle("nopic", 'gonggao');
        wx.setNavigationBarTitle({
          title: '通知列表'
        })
        break;
      case 2:
        this.toggle("title", '资源');
        this.toggle("nopic", 'ziyuan');
        wx.setNavigationBarTitle({
          title: '资源列表'
        })
        break;
      case 4:
        this.toggle("title", '党建');
        this.toggle("nopic", 'dangjian');
        wx.setNavigationBarTitle({
          title: '党建列表'
        })
        break;
    }

  },
  refreshList: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("search.pageNumber", 1);
      that.toggle("search.totalPages", 0);
      that.list();
    });
  },
  list: function(append) {
    var that = this;
    that.toggle("req", false);
    app.req({
      url: "orgmini/org/list",
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          var list = res.data.rows
          for (var item of list) {
            if (item.createTime) {
              var timestamp = Date.parse(new Date());
              var date = new Date(timestamp);
              //获取年份  
              var Y = date.getFullYear();
              if (Y == item.createTime.substring(0, 4)) {
                item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
              } else {
                item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
              }
            }
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("search.totalPages", res.data.totalPages);
          that.toggle("req", true);
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var search = this.data.search;
    if (search.pageNumber < search.totalPages) {
      this.toggle("search.pageNumber", search.pageNumber + 1);
      this.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})